'''
调试子界面类， 使用sub_debug.ui生成的布局
手动触发
'''
import enum
import sys
import os
import time
import traceback
from functools import partial

from PyQt5 import QtWidgets

from PyQt5.QtWidgets import QApplication, QHeaderView, QWidget

from ui.sub_ui.sub_debug import Ui_Form  # 界面控件， QtDesigner生成


from log import logger




class SubDebugWidget(QWidget, Ui_Form):

    '''

    usage:

    '''

    def __init__(self, parent=None, main_ui_handle=None):
        super(SubDebugWidget, self).__init__(parent)

        self.setupUi(self)
        self.main_ui_handle = main_ui_handle

        # 额外样式
        self.setStyleSheet("""
                QPushButton {
                    min-width: 80px;
                    padding: 5px;
                    border-radius: 3px;
                    color: white;
                    background-color: dodgerblue;
                }
                QPushButton:hover {
                    background-color: darkblue;
                }
                
                /* 定义 QGroupBox 样式 */
                QGroupBox {
                    /*font-size: 14px;*/
                    font-weight: bold;
                    border: 1px solid #ccc;
                    border-radius: 8px; /* 使用像素值代替分数 */
                    padding: 1em;
                    margin-top: 1em;
                    background-color: #f5f5f5;
                }
                
                /* 设置标题样式 */
                QGroupBox::title {
                    subcontrol-origin: margin;
                    /*subcontrol-position: top center;*/
                    padding: 0 1em;
                    color: #333;
                    background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
                                                    stop: 0 #f5f5f5, stop: 1 #e5e5e5);
                    border-bottom: 1px solid #ccc;
                    border-top-left-radius: 8px;
                    border-top-right-radius: 8px;
                }
                
                /* 输入框样式 */
                QLineEdit {
                    border: 1px solid #ccc; /* 边框颜色 */
                    border-radius: 4px; /* 边框圆角 */
                    background-color: #fff; /* 背景颜色 */
                }
                /* 输入框焦点样式 */
                QLineEdit:focus {
                    border-color: #80bfff; /* 焦点边框颜色 */
                }
            """)

        self.pushButton_3.clicked.connect(partial(self.munal_trigger_solt, 1))
        self.pushButton_4.clicked.connect(partial(self.munal_trigger_solt, 2))
        self.pushButton_5.clicked.connect(partial(self.munal_trigger_solt, 3))
        self.pushButton_6.clicked.connect(partial(self.munal_trigger_solt, 4))
        self.pushButton_7.clicked.connect(partial(self.munal_trigger_solt, 5))  #



    def munal_trigger_solt(self, step_mode):
        try:

            try:
                # self.step_mode = (self.step_mode) % 5 + 1
                # self.step_mode = 1
                self.main_ui_handle.ss.plcMain.set_manual_trigger(var_name_trigger='p1.is_jc', var_name_step_mode='giPress_VisionMode',
                                                   step_mode=step_mode)
                print(f'manual_trigger_slot {step_mode}')
            except:
                print(traceback.format_exc())

        except:
            print(traceback.format_exc())





if __name__ == '__main__':
    try:
        app = QtWidgets.QApplication(sys.argv)
        mainWnd = SubDebugWidget()
        # ui = Ui_MainWindow()
        # ui.setupUi(mainWnd)
        mainWnd.show()
        sys.exit(app.exec())
    except:
        print(traceback.format_exc())
        logger.error(traceback.format_exc())